package tianyiyun;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main3 {

    public static void main(String[] args) throws Exception{
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        String s = buf.readLine();
        int tv = Integer.parseInt(s);
        String[] ss;
        s = buf.readLine();
        ss = s.split(" ");
        int n = Integer.parseInt(ss[0]);
        int[] useValues = new int[n];
        int[] otherValues = new int[n];
        s = buf.readLine();
        String[] ss2 = s.split(" ");
        for (int i = 1; i <= n; i++){
            useValues[i - 1] = Integer.parseInt(ss[i]);
            otherValues[i - 1] = Integer.parseInt(ss2[i]);
        }
        int[][] dp = new int[n + 1][tv + 1];
        for (int i = 1; i <= n; i++){
            int useValue = useValues[i - 1];
            int otherValue = otherValues[i - 1];
            for (int j = 1; j <= tv; j++){
                if (j >= otherValue){
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - otherValue] + useValue);
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }
        System.out.println(dp[n][tv]);
    }
}
